/*******************************************************************************
 * Copyright (c) 2012 T.Tietjen.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     T.Tietjen - initial API and implementation
 ******************************************************************************/

#include <iostream>
/*#include <core/core.hpp>
 #include <highgui/highgui.hpp>
 #include <cv.h>*/
#include <opencv.hpp>
using namespace std;
using namespace cv;

void drawOptFlowMap(const Mat& flow, Mat& cflowmap, int step, double scale,
		const Scalar& color) {

	for (int y = 0; y < cflowmap.rows; y += step) {
		for (int x = 0; x < cflowmap.cols; x += step) {
			const Point2f& fxy = flow.at<Point2f>(y, x);
			line(cflowmap, Point(x, y),
					Point(cvRound(x + fxy.x), cvRound(y + fxy.y)), color);
			circle(cflowmap, Point(x, y), 2, color, -1);
		}
	}
}

int main() {
	Mat frame1 = imread("C:\\frame3.jpg");
	Mat frame2 = imread("C:\\frame4.jpg");
	Mat flow, grey1, grey2, cflow;
	cv::cvtColor(frame1, grey1, CV_RGB2GRAY, 1);
	cvtColor(frame2, grey2, CV_RGB2GRAY, 1);

	calcOpticalFlowFarneback(grey1, grey2, flow, 0.5, 3, 20, 3, 7, 1.5,
			cv::OPTFLOW_FARNEBACK_GAUSSIAN);
	cvtColor(grey1, cflow, CV_GRAY2BGR);

	drawOptFlowMap(flow, cflow, 4, 1.5, CV_RGB(0, 255, 0));
	imshow("test", cflow);

	cvWaitKey();
	return 0;
}
